using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using StupidSpaceThing;

namespace RacerGame
{
	internal class Sunstar : Unit
	{
		public static new int _ProductionCost = 1000;
		public static new int _ProductionTime = 4000;
		public override int ProductionTime { get { return _ProductionTime; } }
		public override int ProductionCost { get { return _ProductionCost; } }

		public Sunstar(Vector3 position)
			: base(new SunstarEntity(position)) {
			massModifier = 10;			
		}

		public override void Update(RealGameTime gameTime) {
			if( Entered ) {
				if( Target != null ) {
					Entity.ColObject.Body.ApplyAngularImpulse(1.0f);
					Entity.ColObject.Body.AngularVelocity /= 1.04f;
					Entity.Rotation = Quaternion.CreateFromYawPitchRoll(Entity.ColObject.Body.Rotation, 0, 0);
					Entity.UpdateRotationMatrix();
					ApplyForce(Vector2.Normalize(Target.Position2D - Position2D) * 100, false);
					Entity.ColObject.UpdateEntityPosition();
				}
				// optimize
				FactionEntity target = null;
				float targetDistance = float.MaxValue;
				foreach( Faction faction in Faction.Factions ) {
					if( faction == Faction || faction == Faction.None ) continue;
					foreach( FactionEntity fe in faction.FactionEntities ) {
						float curDistance = Vector3.Distance(Position, fe.Position);
						if( target == null || curDistance < targetDistance ) {
							target = fe;
							targetDistance = curDistance;
						}
					}
				}
				Target = target;
			}
			base.Update(gameTime);
		}
	}
}
